package IO;

import org.junit.Test;

import java.io.*;

/**
 *
 */
public class BufferedInputOutputStream {
    //缓冲流读写图片、视频文件
    @Test
    public void test(){
        BufferedInputStream bis = null;
        BufferedOutputStream bos=null;
        try {
            bis = new BufferedInputStream(new FileInputStream(new File("user2.dat")));
            bos = new BufferedOutputStream(new FileOutputStream(new File("user2.dat")));
            int len;
            byte[] bytes=new byte[1024];
            while ((len=bis.read(bytes))!=-1){
                bos.write(bytes,0,len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bis.close();
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //字符流的缓冲流复制文件
    @Test
    public void test1(){
        BufferedReader br = null;
        BufferedWriter bw=null;
        try {
            br = new BufferedReader(new FileReader(new File("user2.dat")));
            bw = new BufferedWriter(new FileWriter(new File("user3.dat")));
            String data;
            //按行读取，没有换行符(也可以用newline()方法)
            while ((data=br.readLine())!=null){
                bw.write(data+"\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
